﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Models;
using SqlSugar;
using System.Reflection;
using System.Text.RegularExpressions;

namespace net8.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class SplitTableController : ControllerBase
    {
        [HttpPost]
        public void Add()
        {
            MySpliteTest model = new MySpliteTest()
            {
                Pk = Guid.NewGuid(),
                Name = GenerateCheckCodeNum(6),
                Time = DateTime.Now
            };
            DbHelper.Db.Insertable(model).SplitTable().ExecuteCommand();
        }

        [HttpGet]
        public List<MySpliteTest> GetList()
        {
            DateTime endDate = DateTime.Now;
            DateTime startDate = endDate.AddDays(-10);
            var list = DbHelper.Db.Queryable<MySpliteTest>().SplitTable(startDate, endDate).ToList();
            //var bbb = DbHelper.Db.Queryable<MySpliteTest>().SplitTable(t=>t.Where(d=>d.Date>startDate)).ToList();

            return list;
        }

        private int rep = 0;

        private string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
    }
}
